package com.apexwear.apexutils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

import com.apexwear.apexutils.R;
import com.apexwear.apexutils.adapter.AdapterSelectDialog;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.yarolegovich.discretescrollview.DSVOrientation;
import com.yarolegovich.discretescrollview.DiscreteScrollView;
import com.yarolegovich.discretescrollview.transform.ScaleTransformer;

import java.util.Calendar;

public class SelectTimeDialog extends Dialog {
    OnPositiveClickListener onPositiveClickListener;
    Context mContext;
    AdapterSelectDialog adapterSelectDialog;
    String[] dataList;
    String title;
    int dialogType;
    Calendar calendar;
    DiscreteScrollView pickerView;

    public SelectTimeDialog(@NonNull Context context, String[] dataList, String title, int dialogType) {
        super(context, R.style.waiteDialog);
        mContext = context;
        this.dataList = dataList;
        this.title = title;
        this.dialogType = dialogType;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_select, null, false);
        TextView titleView = dialogView.findViewById(R.id.dialog_txt);
        titleView.setText(title);
        pickerView = dialogView.findViewById(R.id.picker);
        pickerView.setOrientation(DSVOrientation.VERTICAL);
        pickerView.setItemTransitionTimeMillis(200);
        pickerView.setSlideOnFling(true);
        pickerView.setItemTransformer(new ScaleTransformer.Builder().setMinScale(0.6f).build());
        AppCompatButton sureButton = dialogView.findViewById(R.id.sure);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onPositiveClickListener != null) {
                    onPositiveClickListener.clickButton(dataList[pickerView.getCurrentItem()]);
                }
            }
        });
        adapterSelectDialog = new AdapterSelectDialog(dataList, mContext);
        pickerView.setAdapter(adapterSelectDialog);
        setContentView(dialogView);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void show() {
        super.show();
        if (calendar == null) {
            calendar = Calendar.getInstance();
        }
        String currentYear = null;
        if (dialogType == 0) {
            currentYear = calendar.get(Calendar.YEAR) + "";
        } else if (dialogType == 1) {
            currentYear = (calendar.get(Calendar.MONTH) + 1) + "";
        } else if (dialogType == 2) {
            currentYear = calendar.get(Calendar.DAY_OF_MONTH) + "";
        } else if (dialogType == 3) {
            currentYear = calendar.get(Calendar.HOUR_OF_DAY) + "";
        } else if (dialogType == 4) {
            currentYear = calendar.get(Calendar.MINUTE) + "";
        } else if (dialogType == 5) {
            currentYear = calendar.get(Calendar.SECOND) + "";
        }
        for (int i = 0; i < dataList.length; i++) {
            if (dataList[i].equalsIgnoreCase(currentYear)) {
                pickerView.scrollToPosition(i);
                break;
            }
        }

    }

    public void destroyDialog() {
        mContext = null;
        onPositiveClickListener = null;
        if (adapterSelectDialog != null) {
            adapterSelectDialog.destroyAdapter();
            adapterSelectDialog = null;
        }
        pickerView = null;
        dataList = null;
    }
}
